//
//  MovieViewController.m
//  往昔
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 干晨威. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableView.h"
#import "MovieModel.h"

@interface MovieViewController ()
{
    MovieTableView *_tableView;
    NSArray *_movieNameArr;
}
@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self _createTableView];
    [self _loadData];
    
    [self _createBackButton];
}

- (void)_createTableView {
    
    _tableView = [[MovieTableView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:_tableView];
}

- (void)_loadData {
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"movie1" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
    
    NSDictionary *dataDic = [NSDictionary dictionary];
    dataDic = jsonDic[@"data"];
    NSArray *movieArr = [NSArray array];
    movieArr = dataDic[@"movie"];
    
    self.dataArr = [NSMutableArray array];
    
    for (NSDictionary *dic in movieArr) {
        
        MovieModel *model = [[MovieModel alloc] initWithDictionary:dic error:NULL];
        
        [_dataArr addObject:model];
    }
    _tableView.mutData = _dataArr;
    [_tableView reloadData];
}

//- (void)_loadData {
//    
//    
//    NSString *urlString = @"http://a.apix.cn/apixlife/movie/movie";
//    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    
//    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    
//    [manager.requestSerializer setValue:@"0c06961878734bcf57e1059e77e57f07" forHTTPHeaderField:@"apix-key"];
//
//    self.dataArr = [NSMutableArray array];
//    
//    _movieNameArr = [NSArray arrayWithObjects:@"教父", @"辛德勒的名单", @"公民凯恩", @"肖申克的救赎", @"卡萨布兰卡", @"七武士", @"杀死一只知更鸟", @"美丽人生", @"美国美人", @"飞越疯人院", @"奇爱博士", @"卧虎藏龙", @"后窗", @"夺宝奇兵", @"非常嫌疑犯", @"马耳他之鹰", @"愤怒的公牛", @"记忆碎片", @"沉默的羔羊", @"光荣之路", nil];
////    _movieNameArr = [NSArray arrayWithObjects:@"辛德勒的名单", nil];
//    for (int i = 0; i < _movieNameArr.count; i++) {
//        
//        NSDictionary *param = @{
//                                @"name" :  _movieNameArr[i],
//                                @"page" :  @1,
//                                @"num"  :  @1
//                                };
//        
//        [manager GET:urlString parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
//            
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            
//            [self loadDataDidFinished:responseObject];
//            
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            
//            NSLog(@"error = %@" ,error);
//        }];
//        
//    }
//    
//}

//- (void)loadDataDidFinished:(id)result {
//    
//    NSDictionary *dataDic = [NSDictionary dictionary];
//    dataDic = result[@"data"];
//    NSArray *movieArr = [NSArray array];
//    movieArr = dataDic[@"movie"];
//    
//    for (NSDictionary *dic in movieArr) {
//        
//        MovieModel *model = [[MovieModel alloc] initWithDictionary:dic error:NULL];
//        
//        [_dataArr addObject:model];
//        
//        if ([model.name isEqualToString:@"光荣之路 Glory Road"]) {
//            
//            NSLog(@"%ld", _dataArr.count);
//            
//            [_tableView.mutData addObjectsFromArray:_dataArr];
//            
//            [_tableView reloadData];
//        }
//    }
//    
//}

- (void)_createBackButton {
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [backButton setImage:[UIImage imageNamed:@"item-back-white@2x.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = backItem;
    
}

- (void)backButtonAction:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
